package com.douglas.managesystem.service.student.impl;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.dao.student.InstructMapper;
import com.douglas.managesystem.dao.student.ScoreMapper;
import com.douglas.managesystem.dao.student.StudentMapper;
import com.douglas.managesystem.entity.studentpojo.Score;
import com.douglas.managesystem.entity.studentpojo.Student;
import com.douglas.managesystem.service.student.ScoreService;
import com.douglas.managesystem.vo.ScoreDB;
import com.douglas.managesystem.vo.ScoreDTO;
import com.douglas.managesystem.vo.ScoreStuVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.thymeleaf.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description 分数业务实现
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-16 22:35
 **/
@SuppressWarnings("all")
@Service
public class ScoreServiceImpl implements ScoreService {


    @Autowired
    private ScoreMapper scoreMapper;


    @Autowired
    private InstructMapper instructMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 1.分页查询学生个人的所有成绩信息
     * @param params
     * @return
     */
    @Override
    public PageInfo<ScoreStuVO> findAllStuByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        String name = (String) params.getEntity().get("username");
        List<ScoreDB> list = scoreMapper.queryListByPage(params.getEntity());
        ArrayList<ScoreStuVO> scoreStuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ScoreStuVO scoreStuVO = new ScoreStuVO();
            scoreStuVO.setId(i+1);
            scoreStuVO.setStuNumber(list.get(i).getStuNumber());
            scoreStuVO.setStuName(name);
            scoreStuVO.setSubjectName(list.get(i).getSubjectName());
            scoreStuVO.setScore(list.get(i).getScore());
            scoreStuVO.setCreatedTime(list.get(i).getCreatedTime());
            scoreStuVO.setCredit(list.get(i).getCreditHour());
            scoreStuVO.setSubjectTime(list.get(i).getTotalTime());
            String teacherName = instructMapper.findInstructByClassIdAndLessonId(list.get(i).getSubjectId(),list.get(i).getStuNumber());
            if(StringUtils.isEmpty(teacherName)){
                scoreStuVO.setTeacherName("--");
            }else{
                scoreStuVO.setTeacherName(teacherName);
            }
            scoreStuList.add(scoreStuVO);
        }
        PageInfo<ScoreStuVO> page = new PageInfo<ScoreStuVO>(scoreStuList);
        return page;
    }


    /**
     *  2.分页查询老师所带班级的科目成绩
     * @param params
     * @return
     */
    @Override
    public PageInfo<ScoreDTO> findAllScoreByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        /*List<String> classIds = instructMapper.findAllByTeacherId(params.getEntity().get("teacherId").toString());
        for (String classId : classIds) {
            //班级所有学生
            List<Student> stuList = studentMapper.queryStuByClassId(classId);
            //班级所有课程
            List<String> lessonIds = instructMapper.findAllByClassId(classId);

        }*/

        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<ScoreDTO> list = scoreMapper.queryAllScoreByPage(params.getEntity());
        List<ScoreDTO> listDTO = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                //生物
                Integer biologyScore = null;
                if(list.get(i).getBiologyScore() == null){
                     biologyScore = 0;
                }else{
                     biologyScore = list.get(i).getBiologyScore();
                }
                //化学
                Integer chemistryScore = null;
                if(list.get(i).getChemistryScore() == null){
                    chemistryScore = 0;
                }else {
                    chemistryScore = list.get(i).getChemistryScore();
                }
                //C++
                Integer cPlusScore = null;
                if(list.get(i).getCPlusScore() == null){
                    cPlusScore = 0;
                }else{
                    cPlusScore = list.get(i).getCPlusScore();
                }
                // 历史
                Integer historyScore = null;
                if(list.get(i).getHistoryScore() == null){
                    historyScore = 0;
                }else{
                    historyScore = list.get(i).getHistoryScore();
                }
                // Java
                Integer javaScore = null;
                if(list.get(i).getJavaScore() == null){
                    javaScore = 0;
                }else{
                    javaScore = list.get(i).getJavaScore();
                }
                // 高数
                Integer mathScore = null;
                if(list.get(i).getMathScore() == null){
                    mathScore = 0;
                }else{
                    mathScore = list.get(i).getMathScore();
                }
                // mysql
                Integer mysqlScore = null;
                if(list.get(i).getMysqlScore() == null){
                    mysqlScore = 0;
                }else{
                    mysqlScore = list.get(i).getMysqlScore();
                }
                list.get(i).setSumScore(biologyScore + chemistryScore + cPlusScore
                        + historyScore + javaScore + mathScore + mysqlScore);
                if (list.get(i).getSumScore().intValue() != 0) {
                    listDTO.add(list.get(i));
                }
            }
            if (listDTO.size() > 1) {
                Collections.sort(listDTO, new Comparator<ScoreDTO>() {
                    @Override
                    public int compare(ScoreDTO o1, ScoreDTO o2) {
                        //按照总分进行排序  降序排列  升序 1  0 -1
                        if (o1.getSumScore() > o2.getSumScore()) {
                            return -1;
                        }

                        if (o1.getSumScore().equals(o2.getSumScore())) {
                            return 0;
                        }
                        return 1;
                    }
                });
            }
        }

        PageInfo<ScoreDTO> scoreDTOPageInfo = new PageInfo<>(listDTO);
        return scoreDTOPageInfo;
    }


    /**
     * 3.新增成绩
     * @param scoreDTO
     */
    @Override
    public void saveScore(ScoreDTO scoreDTO) {
        //1.如果学生不存在不进行新增
        String stuNumber = scoreDTO.getStuNumber();
        Student student = studentMapper.queryStuByStuNumber(stuNumber);
        if(student == null){
            return;
        }
        //2.
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("1001001",new BigDecimal(scoreDTO.getJavaScore()));
        map.put("1001002",new BigDecimal(scoreDTO.getCPlusScore()));
        map.put("1001003",new BigDecimal(scoreDTO.getMysqlScore()));
        map.put("1001004",new BigDecimal(scoreDTO.getMathScore()));
        map.put("1001005",new BigDecimal(scoreDTO.getChemistryScore()));
        map.put("1001006",new BigDecimal(scoreDTO.getBiologyScore()));
        map.put("1001007",new BigDecimal(scoreDTO.getHistoryScore()));

        for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
            Score scoreDB = scoreMapper.queryScore(stuNumber,entry.getKey());
            if(scoreDB != null){
                continue;
            }
            Score score = new Score();
            score.setCreatedBy(scoreDTO.getCreatedBy());
            score.setRemark(entry.getKey());
            score.setStuNumber(stuNumber);
            score.setSubjectId(entry.getKey());
            score.setScore(entry.getValue());
            scoreMapper.insert(score);
        }
    }

    /**
     * 4.根据学号查询成绩
     * @param stuNumber
     * @return
     */
    @Override
    public ScoreDTO findScoreByStuNumber(String stuNumber) {
        ScoreDTO scoreDTO = scoreMapper.queryScoreByStuNumber(stuNumber);
        return scoreDTO;
    }


    /**
     * 5.更新成绩
     * @param scoreDTO
     */
    @Override
    public void updateScore(ScoreDTO scoreDTO) {
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("1001001",new BigDecimal(scoreDTO.getJavaScore()));
        map.put("1001002",new BigDecimal(scoreDTO.getCPlusScore()));
        map.put("1001003",new BigDecimal(scoreDTO.getMysqlScore()));
        map.put("1001004",new BigDecimal(scoreDTO.getMathScore()));
        map.put("1001005",new BigDecimal(scoreDTO.getChemistryScore()));
        map.put("1001006",new BigDecimal(scoreDTO.getBiologyScore()));
        map.put("1001007",new BigDecimal(scoreDTO.getHistoryScore()));

        for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
            Score scoreDB = scoreMapper.queryScore(scoreDTO.getStuNumber(),entry.getKey());
            if(scoreDB == null || entry.getValue().intValue()==(scoreDB.getScore().intValue())){
                continue;
            }
            Score score = new Score();
            score.setUpdatedBy(scoreDTO.getUpdatedBy());
            score.setRemark(entry.getKey());
            score.setStuNumber(scoreDTO.getStuNumber());
            score.setSubjectId(entry.getKey());
            score.setScore(entry.getValue());
            scoreMapper.update(score);
        }
    }

    /**
     * 6.删除
     * @param stuNumber
     */
    @Override
    public void delete(String stuNumber) {
        scoreMapper.delete(stuNumber);
    }
}
